/*Gisca*/
/******** menu customization **********/

$(document).ready(function(){

    $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)

    $("ul.topnav li").click(function() { //When trigger is clicked...

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).hover(function() {
            }, function(){
                $(this).find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
            });

    //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        $(this).addClass("subhover"); //On hover over, add class "subhover"
    }, function(){	//On Hover Out
        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
    });

    $("ul.gallery li").hover(function() { //On hover...

        var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'

        //Set a background image(thumbOver) on the <a> tag - Set position to bottom
        $(this).find("a.thumb").css({
            'background' : 'url(' + thumbOver + ') no-repeat center bottom'
        });

        //Animate the image to 0 opacity (fade it out)
        $(this).find("span").stop().fadeTo('normal', 0 , function() {
            $(this).hide() //Hide the image after fade
        });
    } , function() { //on hover out...
        //Fade the image to full opacity
        $(this).find("span").stop().fadeTo('normal', 1).show();
    });

    //$('#midbar').dropShadow({left:-1,top:4,blur:3});
    $('#lowerU').dropShadow({left:0,top:3,blur:3});
    $('#upperDL').dropShadow({left:0,top:3,blur:2});
    $('#upperDR').dropShadow({left:0,top:3,blur:2});
    
    $('#imageSlide').crossSlide({
        sleep:3,
        fade: 1
    }, [
    {
        src:  'static/image/head.jpg',
        alt:  'Sand Castle',
        from: '100% 80% 1x',
        to:   '100% 0% 1.7x',
        time: 7
    }, {
        src:  'static/image/hd2.jpg',
        alt:  'Sunflower',
        from: 'top left',
        to:   'bottom right 1.5x',
        time: 7
    }, {
        src:  'static/image/hd3.jpg',
        alt:  'Flip Flops',
        from: '100% 80% 1.5x',
        to:   '80% 0% 1.1x',
        time: 7
    }, {
        src:  'static/image/hd4.jpg',
        alt:  'Sunflower',
        from: 'top left',
        to:   'bottom right 1.5x',
        time: 7
    }, {
        src:  'static/image/hd1.jpg',
        alt:  'Sunflower',
        from: 'top left',
        to:   'bottom right 1.5x',
        time: 7
    }, {
        src:  'static/image/hd5.jpg',
        alt:  'Rubber Ring',
        from: '100% 50%',
        to:   '30% 50% 1.5x',
        time: 7
    }
    ], function(idx, img, idxOut, imgOut) {
        if (idxOut == undefined)
        {
            // starting single image phase, put up caption
            $('div.caption').text(img.alt).animate({
                opacity: .7
            })
        }
        else
        {
            // starting cross-fade phase, take out caption
            $('div.caption').fadeOut()
        }
    });
    
});

/********* registration validation *********/

function validasiNama(nama)
{
    var regex = new RegExp("^[A-Za-z ]{5}[A-Za-z ]*");
    var regex2 = new RegExp("[^A-Za-z ]");
    var validasinama = document.getElementById("validasinama");

    if(nama.match(regex2)!=null && nama != "")
    {
        validasinama.innerHTML = "Name cannot contain symbol or number";
        document.getElementById("validasinm").value = "not valid";
    }
    else if(nama.match(regex)==null && nama.match(regex2)==null && nama != "")
    {
        validasinama.innerHTML = "Name at least 5 character long";
        document.getElementById("validasinm").value = "not valid";
    }
    else
    {
        validasinama.innerHTML = "";
        document.getElementById("validasinm").value = "";
    }
}

function validasiUsername(username)
{
    var regex = new RegExp("^[A-Za-z0-9._]{5}[A-Za-z0-9._]*");
    var regex2 = new RegExp("[^A-Za-z0-9._]");

    var validasiusername = document.getElementById("validasiusername");
    if(username.match(regex2)!=null && username != "")
    {
        validasiusername.innerHTML = "Username cannot contain space or symbol";
        document.getElementById("validasiusr").value = "not valid";
    }
    else if(username.match(regex)==null && username.match(regex2)==null && username != "")
    {
        validasiusername.innerHTML = "Username at least 5 character long";
        document.getElementById("validasiusr").value = "not valid";
    }
    else
    {
        validasiusername.innerHTML = "";
        document.getElementById("validasiusr").value = "";
    }
}

function validasiPassword(password)
{
    var regex = new RegExp("^.{6}.*");

    var validasipassword = document.getElementById("validasipassword");

    if(password.match(regex)==null && password != "")
    {
        validasipassword.innerHTML = "Password at least 6 character";
        document.getElementById("validasipass").value = "not valid";
    }
    else
    {
        validasipassword.innerHTML = "";
        document.getElementById("validasipass").value = "";
    }
}

function validasiPasswordLagi(password,passwordlagi)
{
    //var password = document.getElementById("passwordPengguna");
    var validasipasswordlagi = document.getElementById("validasipasswordlagi");

    if(password != passwordlagi && passwordlagi != "")
    {
        validasipasswordlagi.innerHTML = "Password tidak cocok";
        document.getElementById("validasipasslagi").value = "not valid";
    }
    else
    {
        validasipasswordlagi.innerHTML = "";
        document.getElementById("validasipasslagi").value = "";
    }
}

function validasiEmail(email)
{
    var emailaddr = email.toString().split("@", 2);
    var regex = new RegExp("^[A-Za-z0-9._]{5}[A-Za-z0-9._]*");
    var regex2 = new RegExp("[a-z]+([.][a-z]{2}[a-z]*)+$");
    var regex3 = new RegExp("[^A-Za-z0-9._]+");
    var regex4 = new RegExp("[^a-z.]+");

    var validasiemail = document.getElementById("validasiemail");
    if(emailaddr[0] != null && emailaddr[1] != null)
    {
        if(emailaddr[0].match(regex3) != null)
        {
            validasiemail.innerHTML = "This email is not valid";
            document.getElementById("validasimail").value = "not valid";
        }
        else if(emailaddr[0].match(regex) == null)
        {
            validasiemail.innerHTML = "Email head at least 5 character long";
            document.getElementById("validasimail").value = "not valid";
        }
        else
        {
            if(emailaddr[1].match(regex4) != null && emailaddr[1] != "")
            {
                validasiemail.innerHTML = "Domain/extention is not valid";
                document.getElementById("validasimail").value = "not valid";
            }
            else if(emailaddr[1].match(regex2) == null && emailaddr[1] != "")
            {
                validasiemail.innerHTML = "Extention at least 2 character long ";
                document.getElementById("validasimail").value = "not valid";
            }
            else
            {
                validasiemail.innerHTML = "";
                document.getElementById("validasimail").value = "";
            }
        }
    }
    else if(emailaddr[1] == null)
    {
        validasiemail.innerHTML = "This email is not valid";
        document.getElementById("validasimail").value = "not valid";
    }
    else
    {
        validasiemail.innerHTML = "";
    }
}

function validasiEmailLagi(emaillagi)
{
    var email = document.getElementById("email");
    var validasiemaillagi = document.getElementById("validasiemaillagi");

    if(email.value != emaillagi && emaillagi != "")
    {
        validasiemaillagi.innerHTML = "Email didn't match";
        document.getElementById("validasipasslagi").value = "not valid";
    }
    else
    {
        validasiemaillagi.innerHTML = "";
        document.getElementById("validasiemllagi").value = "";
    }
}

function validasiNoTelp(notelp)
{
    var regex = new RegExp("^[\u002B]?[0-9]{7}[0-9]*");
    var regex2 = new RegExp("(\D)*");

    var validasinotelp = document.getElementById("validasinotelp");

    if(notelp.match(regex)==null && notelp.match(regex2)==null && notelp != "")
    {
        validasinotelp.innerHTML = "Phone number at least contain 7 digit";
        document.getElementById("validasitelp").value = "not valid";
    }
    else if (notelp.match(regex2) != null && notelp.match(regex) == null && notelp != "")
    {
        validasinotelp.innerHTML = "Phone number should not contain charater";
        document.getElementById("validasitelp").value = "not valid";
    }
    else
    {
        validasinotelp.innerHTML = "";
        document.getElementById("validasitelp").value = "";
    }
}

function validasiAlamat(alamat)
{
    var regex = new RegExp("^[A-Za-z0-9., ]{10}[A-Za-z0-9./ ]*");
    var regex2 = new RegExp("[^A-Za-z0-9., ]");

    var validasialamat = document.getElementById("validasialamat");
    if(alamat.match(regex2)!=null && alamat != "")
    {
        validasialamat.innerHTML = "Address is not valid";
        document.getElementById("validasiaddr").value = "not valid";
    }
    else if(alamat.match(regex)==null && alamat.match(regex2)==null && alamat != "")
    {
        validasialamat.innerHTML = "Address at least contain 10 character";
        document.getElementById("validasiaddr").value = "not valid";
    }
    else
    {
        validasialamat.innerHTML = "";
        document.getElementById("validasiaddr").value = "";
    }
}

function crosscheck()
{
    
    if(/*document.getElementById("usernameEdit").value != "" &&
        */document.getElementById("validasialamat").innerHTML == "" &&
        document.getElementById("validasinotelp").innerHTML == "" &&
        document.getElementById("validasiemail").innerHTML == "" &&
        document.getElementById("validasinama").innerHTML == "" &&
        document.getElementById("validasipasswordlagi").innerHTML == "" &&
        document.getElementById("validasipassword").innerHTML == "" &&
        document.getElementById("validasiusername").innerHTML == "")
        {        
        document.formEditPengguna.submit();
        alert("Perubahan Data Diri Berhasil ^^");
    }
    else
    {
        alert("Maaf, data yang Anda masukkan tidak valid. Mohon isi kembali form.");
    }
}

function crosscheckTambah()
{

    if(document.getElementById("username").value != "" &&
        document.getElementById("validasialamat").innerHTML == "" &&
        document.getElementById("validasinotelp").innerHTML == "" &&
        document.getElementById("validasiemail").innerHTML == "" &&
        document.getElementById("validasinama").innerHTML == "" &&
        document.getElementById("validasipasswordlagi").innerHTML == "" &&
        document.getElementById("validasipassword").innerHTML == "" &&
        document.getElementById("validasiusername").innerHTML == "")
        {
        document.formTambahPengguna.submit();
        alert("Penambahan Data Diri Pengguna Baru Berhasil ^^");
    }
    else
    {
        alert("Maaf, data yang Anda masukkan tidak valid. Mohon isi kembali form.");
    }
}

function submitform()
{
    document.getElementsByName("formEditPengguna").submit();
}

/******* Pop up ********/

//0 means disabled; 1 means enabled;
var popupStatus = 0;

//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;

    var popupHeight1 = $("#popupContact").height();
    var popupWidth1 = $("#popupContact").width();
    var popupHeight2 = $("#popupRole").height();
    var popupWidth2 = $("#popupRole").width();
    var popupHeight3 = $("#popupDelete").height();
    var popupWidth3 = $("#popupDelete").width();
    //centering
    $("#popupContact").css({
        "position": "fixed", // dirubah sama sesdika :D
        "top": windowHeight/2-popupHeight1/2,
        "left": windowWidth/2-popupWidth1/2
    });
    $("#popupRole").css({
        "position": "fixed",
        "top": windowHeight/2-popupHeight2/2,
        "left": windowWidth/2-popupWidth2/2
    });
    $("#popupDelete").css({
        "position": "fixed",
        "top": windowHeight/2-popupHeight3/2,
        "left": windowWidth/2-popupWidth3/2
    });
    //only need force for IE6
    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

//message
function popupMessage(msg) {
    alert(msg);
}

function PopUpHapus (userid)
{
    $username = userid;
    document.getElementById("del").innerHTML = '<label id="useridDel">Apa anda yakin ingin menghapus ' + $username + ' ?</label>';
    document.getElementById("namaInput").value = userid;

    centerPopup();
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

function konfirmasiRole()
{
    var role = "";
    var notrole= "";
    var pemisah = ",";
    
    if(document.getElementById("Admin").checked == true)
    {
        role = role + document.getElementById("Admin").value + pemisah;
    }
    if(document.getElementById("TU").checked == true)
    {
        role = role + document.getElementById("TU").value + pemisah;
    }
    if(document.getElementById("Dosen").checked == true)
    {
        role = role + document.getElementById("Dosen").value + pemisah;
    }
    if(document.getElementById("Mahasiswa").checked == true)
    {
        role = role + document.getElementById("Mahasiswa").value + pemisah;
    }
    if(document.getElementById("Dekan").checked == true)
    {
        role = role + document.getElementById("Dekan").value + pemisah;
    }

    dataRole = role.split(",");
    document.getElementById("daftarRole").value = dataRole;


    if(document.getElementById("Admin").checked == false)
    {
        notrole = notrole + document.getElementById("Admin").value + pemisah;
    }
    if(document.getElementById("TU").checked == false)
    {
        notrole = notrole + document.getElementById("TU").value + pemisah;
    }
    if(document.getElementById("Dosen").checked == false)
    {
        notrole = notrole + document.getElementById("Dosen").value + pemisah;
    }
    if(document.getElementById("Mahasiswa").checked == false)
    {
        notrole = notrole + document.getElementById("Mahasiswa").value + pemisah;
    }
    if(document.getElementById("Dekan").checked == false)
    {
        notrole = notrole + document.getElementById("Dekan").value + pemisah;
    }

    dataNotRole = notrole.split(",");
    document.getElementById("daftarNotRole").value = dataNotRole;
}

//loading popup with jQuery magic!
function loadPopup(id, nama,deskripsi,gambar)
{
    //loads popup only if it is disabled
    document.getElementById("reserved").innerHTML = '<strong id="reserved">' + nama + '</strong><div style="width: 170px; height: 120px; overflow: auto;"><br/>' + deskripsi + '</div>';
    document.getElementById("imgPopUp").src = "static/image/" + gambar;
    document.getElementById("idRoom").value = id;
    document.getElementById("namaRoom").value = nama;
    centerPopup();
    $("#tglf").datepicker({
        dateFormat: 'yy-mm-dd'
    });
    $("#tglt").datepicker({
        dateFormat: 'yy-mm-dd'
    });
    if(popupStatus==0)
    {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        //$("#backgroundPopup").fadeIn("slow");
        //$("#popupContact").fadeIn("slow");
        $("#backgroundPopup").slideToggle("slow", callback);
        $("#popupContact").slideDown("slow", callback);
        popupStatus = 1;
    }
}

//loading popup with jQuery magic!
function loadPopupOthServ(id, nama,deskripsi,gambar)
{
    //loads popup only if it is disabled
    document.getElementById("reserved").innerHTML = '<strong id="reserved">' + nama + '</strong><div style="width: 275px; height: 120px; overflow: auto;"><br/>' + deskripsi + '</div>';
    document.getElementById("imgPopUp").src = "static/image/" + gambar;
    document.getElementById("idOtherServ").value = id;
    document.getElementById("namaServices").value = nama;
    centerPopup();
    $("#tglf").datepicker({
        dateFormat: 'yy-mm-dd'
    });
    $("#tglt").datepicker({
        dateFormat: 'yy-mm-dd'
    });
    if(popupStatus==0)
    {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        //$("#backgroundPopup").fadeIn("slow");
        //$("#popupContact").fadeIn("slow");
        $("#backgroundPopup").slideToggle("slow", callback);
        $("#popupContact").slideDown("slow", callback);
        popupStatus = 1;
    }
}

function callback()
{
}

function loadPopupRole(id,username)
{
    document.getElementById('idUserRole').value = id;
    document.getElementById('userRole').innerHTML = username;
    centerPopup();
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupRole").fadeIn("slow");
        popupStatus = 1;
    }
}

function editRole()
{
    document.newRole.submit();
    disablePopup();
    document.getElementById('idUserRole').value = "";
    document.getElementById('userRole').innerHTML = "";
}

function loadPopupDelete(id,username)
{
    document.getElementById('idDelUser').value = id;
    centerPopup();
    if(popupStatus==0)
    {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupDelete").fadeIn("slow");
        popupStatus = 1;
    }
}

function loadPopupUploadRoomImage ()
{
    var head = document.getElementById("head");
    head.innerHTML = "Upload Image";
    centerPopup();
    if(popupStatus==0)
    {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}


function deleteUser(id)
{
    iddel = document.getElementById('idDelUser').value;
    if(id != iddel){
        document.delUser.submit();
        disablePopup();
        document.getElementById('idDelUser').value = "";
    } else{
        alert("Account is currently in use, you can't delete this account");
    }

}

//disabling popup with jQuery magic!
function disablePopup()
{
    //disables popup only if it is enabled
    if(popupStatus==1)
    {
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        $("#popupRole").fadeOut("slow");
        $("#popupDelete").fadeOut("slow");
        popupStatus = 0;
    }
}

function submit()
{
    //disables popup only if it is enabled
    if(popupStatus==1)
    {
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
    document.room.submit();
}

$(document).ready(function()
{
    $("#button").click(function()
    {
        loadPopup();
    });
    $("#popupContactClose").click(function()
    {
        disablePopup();
    });
    $("#paymentdate").datepicker(
    {
        dateFormat: 'yy-mm-dd'
    });
    $("#addTglMsk").datepicker(
    {
        dateFormat: 'yy-mm-dd'
    });
    $("#addTglKlr").datepicker(
    {
        dateFormat: 'yy-mm-dd'
    });
    $("#editTglMsk").datepicker(
    {
        dateFormat: 'yy-mm-dd'
    });
    $("#editTglKlr").datepicker(
    {
        dateFormat: 'yy-mm-dd'
    });
    $("#backgroundPopup").click(function()
    {
        disablePopup();
    });
    $(document).keypress(function(e)
    {
        if(e.keyCode==27 && popupStatus==1)
        {
            disablePopup();
        }
    });
});

function updatePass(){
    if(document.getElementById("validasialamat").innerHTML == "" &&
        document.getElementById("validasinotelp").innerHTML == "" &&
        document.getElementById("validasiemaillagi").innerHTML == "" &&
        document.getElementById("validasiemail").innerHTML == "" &&
        document.getElementById("validasipasswordlagi").innerHTML == "" &&
        document.getElementById("validasipassword").innerHTML == "" &&
        document.getElementById("validasinama").innerHTML == ""){
        document.passwordUpdate.submit();
    }else{
        alert("field is not valid");
    }
}

function updateName(){
    if(document.getElementById("validasialamat").innerHTML == "" &&
        document.getElementById("validasinotelp").innerHTML == "" &&
        document.getElementById("validasiemaillagi").innerHTML == "" &&
        document.getElementById("validasiemail").innerHTML == "" &&
        document.getElementById("validasipasswordlagi").innerHTML == "" &&
        document.getElementById("validasipassword").innerHTML == "" &&
        document.getElementById("validasinama").innerHTML == ""){
        document.nameUpdate.submit();
    }else{
        alert("field is not valid");
    }
}

function updateAddress(){
    if(document.getElementById("validasialamat").innerHTML == "" &&
        document.getElementById("validasinotelp").innerHTML == "" &&
        document.getElementById("validasiemaillagi").innerHTML == "" &&
        document.getElementById("validasiemail").innerHTML == "" &&
        document.getElementById("validasipasswordlagi").innerHTML == "" &&
        document.getElementById("validasipassword").innerHTML == "" &&
        document.getElementById("validasinama").innerHTML == ""){
        document.addressUpdate.submit();
    }else{
        alert("field is not valid");
    }
}

function updatePhone(){
    if(document.getElementById("validasialamat").innerHTML == "" &&
        document.getElementById("validasinotelp").innerHTML == "" &&
        document.getElementById("validasiemaillagi").innerHTML == "" &&
        document.getElementById("validasiemail").innerHTML == "" &&
        document.getElementById("validasipasswordlagi").innerHTML == "" &&
        document.getElementById("validasipassword").innerHTML == "" &&
        document.getElementById("validasinama").innerHTML == ""){
        document.phoneUpdate.submit();
    }else{
        alert("field is not valid");
    }
}

function updateEmail(){
    if(document.getElementById("validasialamat").innerHTML == "" &&
        document.getElementById("validasinotelp").innerHTML == "" &&
        document.getElementById("validasiemaillagi").innerHTML == "" &&
        document.getElementById("validasiemail").innerHTML == "" &&
        document.getElementById("validasipasswordlagi").innerHTML == "" &&
        document.getElementById("validasipassword").innerHTML == "" &&
        document.getElementById("validasinama").innerHTML == ""){
        document.emailUpdate.submit();
    }else{
        alert("field is not valid");
    }
}


